﻿Imports System.Windows.Threading
Imports System.ComponentModel.Composition

<AddInExport("{a29af3a2-5e9e-4a36-88e8-35535096158e}", "Simple Timer",
    Description:="The simple implementation of MISAO's AddIn.",
    HasSetting:=True, HasWindow:=False,
    IconUri:="", Publisher:="pubs")> _
<Export(GetType(ISourceAddIn))>
Public Class SimpleTimerSource
    Implements ISourceAddIn

    'memo. change itemgroup in .vbproj
    '
    '[before]
    '<ItemGroup>
    '  <ApplicationDefinition Include="SettingWindow.xaml">
    '    <Generator>MSBuild:Compile</Generator>
    '    <SubType>Designer</SubType>
    '  </ApplicationDefinition>
    '</ItemGroup>
    '
    '[after]
    '<ItemGroup>
    '  <Page Include="SettingWindow.xaml">
    '    <Generator>MSBuild:Compile</Generator>
    '    <SubType>Designer</SubType>
    '  </Page>
    '</ItemGroup>

    Private Activated As Boolean
    Private MyTimer As DispatcherTimer
    Private MySetting As ISettings

    Private Sub Start()

        Dim IntervalTime As Integer
        If Not MySetting.TryGetValue("Interval", IntervalTime) Then
            IntervalTime = 1000
        End If

        MyTimer = New DispatcherTimer()
        MyTimer.Interval = New TimeSpan(0, 0, 0, 0, IntervalTime)

        AddHandler MyTimer.Tick, Sub(parent, args)
                                     Me.SendTime()
                                 End Sub
        MyTimer.Start()

        Activated = True

    End Sub

    Private Sub SendTime()

        Me.PostMessage.Invoke(Me, New Message() With {
                              .Text = DateTime.Now.ToString("HH:mm:ss")})

    End Sub

    Private Sub Reset()

        If MyTimer IsNot Nothing Then
            MyTimer.Stop()
        End If

        Activated = False

    End Sub

#Region "ISourceAddIn Members"

    Public Sub Initialize(ByVal settings As ISettings) Implements IAddIn.Initialize
        MySetting = settings
    End Sub

    Public Sub Activate() Implements IAddIn.Activate

        If Not Activated Then
            Me.Start()
        End If

    End Sub

    Public Sub Deactivate() Implements IAddIn.Deactivate

        If Activated Then
            Me.Reset()
        End If

    End Sub

    Public Event Deactivated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAddIn.Deactivated

    Public Sub ShowDialog(ByVal owner As System.Windows.Window) Implements IAddIn.ShowDialog

        Dim w As New SettingWindow()
        Dim d As New ViewModel.SettingWindowViewModel()
        MySetting.TryGetValue("Interval", d.Interval)

        w.Owner = owner
        w.DataContext = d
        AddHandler w.Closed, Sub(sender As Object, e As EventArgs)
                                 MySetting("Interval") = d.Interval
                             End Sub
        w.Show()
    End Sub

    <Import(GetType(PostMessageCallback))>
    Public Property PostMessage As PostMessageCallback Implements ISourceAddIn.PostMessage

    <Import(GetType(WriteLogCallback))>
    Public Property WriteLog As WriteLogCallback Implements ISourceAddIn.WriteLog

#End Region

End Class
